package com.bkhech.home.practice.spring.el;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;

import java.util.ArrayList;
import java.util.List;

/**
 * EvaluationContext Demo
 *
 * @author guowm
 * @date 2023/1/16
 */
public class EvaluationContextDemo {

    static class Simple {
        public List<Boolean> booleanList = new ArrayList<>();
    }

    public static void main(String[] args) {
        ExpressionParser parser = new SpelExpressionParser();

        // EvaluationContext
        Simple simple = new Simple();
        simple.booleanList.add(true);
        final EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
        String expStr = "booleanList[0]";
        final Expression expression = parser.parseExpression(expStr);
        expression.setValue(context, simple, "false");
        final Boolean bool = simple.booleanList.get(0);
        // bool is false
        System.out.println(bool);
    }

}
